package com.team.recruitment_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.team.recruitment_management_system.pojo.entity.RecruitProcess;
import com.team.recruitment_management_system.pojo.entity.Resume;
import com.team.recruitment_management_system.pojo.vo.RecruitProcessVo;
import com.team.recruitment_management_system.security.OperationLogAnnotation;
import com.team.recruitment_management_system.service.RecruitProcessService;
import com.team.recruitment_management_system.service.ResumeService;
import com.team.recruitment_management_system.util.JwtHelper;
import com.team.recruitment_management_system.util.result.Result;
import com.team.recruitment_management_system.util.result.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author xxxty
 * time 2024.4.3
 */
@RequestMapping("/recruitProcess")
@RestController
@Tag(name = "招聘流程")
public class RecruitProcessController {
    @Autowired
    RecruitProcessService recruitProcessService;
    @Autowired
    ResumeService resumeService;
    @Autowired
    private JwtHelper jwtHelper;

    @PostMapping("/add")
    @Operation(summary = "添加招聘流程", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "招聘流程", operType = "添加", operDesc = "往招聘流程表添加数据")
    public Result add(@RequestBody RecruitProcess recruitProcess, @RequestHeader String Authorization) {
        Integer userId = jwtHelper.getUserId(Authorization);
        LambdaQueryWrapper<Resume> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Resume::getUserId, userId);
        Resume resume = resumeService.getOne(lambdaQueryWrapper);
        recruitProcess.setResumeId(resume.getId());
        recruitProcess.setStatus(1);
        return recruitProcessService.insert(recruitProcess);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除招聘流程", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "招聘流程", operType = "删除", operDesc = "从招聘流程表删除数据")
    public Result delete(@PathVariable("id") int id) {
        return recruitProcessService.removeById(id) ? Result.ok("删除成功") : Result.build("删除失败", ResultCodeEnum.DATABASE_ERROR);
    }

    @PutMapping("/update")
    @Operation(summary = "更新招聘流程", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "招聘流程", operType = "更新", operDesc = "从招聘流程表更新数据")
    public Result update(@RequestBody RecruitProcess recruitProcess) {
        return recruitProcessService.updateById(recruitProcess) ? Result.ok("更新成功") : Result.build("更新失败", ResultCodeEnum.DATABASE_ERROR);
    }

    @GetMapping("/getMine")
    @Operation(summary = "查询我的投递", security = {@SecurityRequirement(name = "Authorization")})
    public Result getMineRecruitProcess(@RequestHeader String Authorization) {
        return Result.ok(recruitProcessService.getMineRecruitProcess(Authorization));
    }

    @GetMapping("/getByid/{id}")
    @Operation(summary = "查询招聘流程", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "招聘流程", operType = "查询", operDesc = "从招聘流程表查询数据")
    public Result get(@PathVariable("id") int id) {
        return Result.ok(recruitProcessService.getById(id));
    }

    @GetMapping("/getByresumeId/{resumeId}")
    @Operation(summary = "查询招聘流程", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "招聘流程", operType = "查询", operDesc = "通过简历id查询招聘流程")
    public Result getByresumeId(@PathVariable("resumeId") int id) {
        return Result.ok(recruitProcessService.getByresumeId(id));
    }

    @GetMapping("/getByjobId/{jobId}")
    @Operation(summary = "查询招聘流程", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "招聘流程", operType = "查询", operDesc = "通过工作id查询招聘流程")
    public Result getByjobId(@PathVariable("jobId") int id) {
        return Result.ok(recruitProcessService.getByjobId(id));
    }

    @GetMapping("/getBystatus/{status}")
    @Operation(summary = "查询招聘流程", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "招聘流程", operType = "查询", operDesc = "通过status查询招聘流程")
    public Result getBystatus(@PathVariable("status") int id) {
        return Result.ok(recruitProcessService.getBystatus(id));
    }

    @GetMapping("/gethrResume/{id}")
    @Operation(summary = "获取hr收到的简历", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "招聘模块", operType = "查询", operDesc = "获取hr收到的简历")
    public Result<List<Resume>> getResumePage(@PathVariable("id") int hrId) {
        List<Integer> list = recruitProcessService.getResume(hrId);
        return Result.ok(resumeService.listByIds(list));
    }

    @GetMapping("/getrecruitProcess/{id}")
    @Operation(summary = "获取hr的招聘流程", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "招聘模块", operType = "查询", operDesc = "获取hr的招聘流程")
    public Result<List<RecruitProcessVo>> getrecruitProcess(@PathVariable("id") int hrId) {
        List<RecruitProcessVo> list = recruitProcessService.getRecruitProcess(hrId);
        return Result.ok(list);
    }
}

